package com.zimmur.app.mocolink.manage.web.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.zimmur.app.mocolink.domain.model.Goods;
import com.zimmur.app.mocolink.domain.param.GoodsParamForManage;
import com.zimmur.app.mocolink.domain.util.PageUtil;
import com.zimmur.app.mocolink.domain.vo.GoodsBrandInfo;
import com.zimmur.app.mocolink.domain.vo.GoodsCategoryVo;
import com.zimmur.app.mocolink.domain.vo.GoodsDetailVo;
import com.zimmur.app.mocolink.domain.vo.GoodsForManageInfo;
import com.zimmur.app.mocolink.domain.vo.GoodsForManageVo;
import com.zimmur.app.mocolink.domain.vo.GoodsInfoVo;
import com.zimmur.app.mocolink.manage.web.shiro.token.TokenManager;
import com.zimmur.app.mocolink.service.IGoodsBrandService;
import com.zimmur.app.mocolink.service.IGoodsCategoryService;
import com.zimmur.app.mocolink.service.IGoodsService;

@Controller
@RequestMapping("/goods")
public class GoodsController extends BaseController{
	private Logger log= LoggerFactory.getLogger(GoodsController.class);
	@Resource
	private IGoodsService goodsService;
	@Resource
	private IGoodsBrandService brandService;
	@Resource
	private IGoodsCategoryService categoryService;
	@RequestMapping("/index")
	public  String index(Model model){
		Integer pageNo=getParam("pageNo", 1);
		//1.查询品牌信息
		List<GoodsBrandInfo> brandItemList = brandService.queryEntireBrand();
		//2.查询品类信息
		List<GoodsCategoryVo> categoryItemList = categoryService.queryEntireCategory();
		//3.查询商品信息
		GoodsParamForManage param=new GoodsParamForManage(pageNo);
		GoodsForManageVo page=goodsService.queryGoodsForManage(param);
		page.setBrandItemList(brandItemList);
		page.setCategoryItemList(categoryItemList);
		model.addAttribute("param", param);
		model.addAttribute("page", page);
		return "/center/goods/list";
	}
	/**
	 * 综合检索商品信息
	 * @return
	 */
	@RequestMapping("/searchGoodsInfo")
	public  String searchGoods(Model model){
		Integer pageNo=getParam("pageNo", 1);
		Integer pageSize=getParam("pageSize", 10);
		Integer brandId = getIntParam("brandId");
		Integer categoryId=getIntParam("categoryId");
		Short isSp= getShortParam("isSp");
		String state=getParam("goodsStatus");
		String keyword=getParam("keyword");
		String timeStart=getParam("timeStart");
		String timeEnd=getParam("timeEnd");
		//1.查询品牌信息
		List<GoodsBrandInfo> brandItemList = brandService.queryEntireBrand();
		//2.查询品类信息
		List<GoodsCategoryVo> categoryItemList = categoryService.queryEntireCategory();
		//3.查询商品信息
		GoodsParamForManage param=new GoodsParamForManage(pageNo, keyword, categoryId,brandId,isSp, state, timeStart, timeEnd);
		param.setPageSize(pageSize);
		GoodsForManageVo page=goodsService.queryGoodsForManage(param);
		page.setBrandItemList(brandItemList);
		page.setCategoryItemList(categoryItemList);
		model.addAttribute("param", param);
		model.addAttribute("page", page);
		return "/center/goods/list";
	}
	@RequestMapping("/add")
	public  String add(Model model){
		//1.查询品牌信息
		List<GoodsBrandInfo> brandItemList = brandService.queryEntireBrand();
		//2.查询商品类型信息
		List<GoodsCategoryVo> categoryItemList = categoryService.queryEntireCategory();
		GoodsInfoVo vo = new GoodsInfoVo();
		Goods goods=new Goods();
		vo.setGoods(goods);
		vo.setCategoryItemList(categoryItemList);
		vo.setBrandItemList(brandItemList);
		model.addAttribute("goodsVo", vo);
		return "/center/goods/edit";
	}
	/**
	* <p>Title: editGoods</p>
	* <p>Description: 初始化编辑页面</p>
	* @param model
	* @return
	 */
	@RequestMapping("/edit")
	public  String edit(Model model){
		Integer goodsId  = getParam("goodsId",0);
		//1.查询品牌信息
		List<GoodsBrandInfo> brandItemList = brandService.queryEntireBrand();
		//2.查询商品类型信息
		List<GoodsCategoryVo> categoryItemList = categoryService.queryEntireCategory();
		GoodsInfoVo vo =null;
		
		vo = goodsService.queryGoodsInfo(goodsId);
		vo.setCategoryItemList(categoryItemList);
		vo.setBrandItemList(brandItemList);
		
		model.addAttribute("goodsVo", vo);
		return "/center/goods/edit";
	}
	/**
	* <p>Title: updateStatus</p>
	* <p>Description: 变更商品状态</p>
	* @param model
	* @return
	 */
	@ResponseBody
	@RequestMapping("/updateStatus")
	public  Map<String,String> updateStatus(Model model){
		Map<String,String> map=new HashMap<String, String>();
		//获取用户名称
		String userName = TokenManager.getNickname();
		Integer goodsId= getParam("goodsId", 0);
		String status = getParam("status");
		int flag = goodsService.changeGoodsStatus(goodsId, status,userName);
		if(flag>0){
			map.put("code","000000");
			map.put("msg", "操作成功");
		}else{
			map.put("code","100000");
			map.put("msg", "操作失败");
		}
		return map;
	}
	/**
	* <p>Title: saveGoods</p>
	* <p>Description:保存商品信息 </p>
	* @param model
	* @return
	 */
	@ResponseBody
	@RequestMapping("/save")
	public  Map<String,String> save(Model model){
		Map<String,String> map=new HashMap<String, String>();
	    String goodsDetail=getParam("goodsDetail");
		String userName = TokenManager.getNickname(); 
	    GoodsDetailVo vo = JSONObject.parseObject(goodsDetail, GoodsDetailVo.class);
	    int flag=0;
	    Integer goodsId = vo.getId();
	    if(goodsId!=null&&goodsId>0){//修改商品信息保存数据
	    	flag = goodsService.updateGoodsDetailForManage(vo,userName);
	    }else{//新增商品信息保存数据
	    	flag = goodsService.saveGoodsDetailForManage(vo,userName);
	    }
		if(flag>0){
			map.put("code","000000");
			map.put("msg", "操作成功");
		}else{
			map.put("code","100000");
			map.put("msg", "操作失败");
		}
		return map;
	}
	@RequestMapping("/queryGoodsForChoose")
	public String queryGoodsForChoose(Model model){
		Integer pageNo=getParam("pageNo", 1);
		Integer pageSize=getParam("pageSize", 10);
		Integer brandId = getIntParam("brandId");
		Integer categoryId=getIntParam("categoryId");
		Short isSp= getShortParam("isSp");
		String state=getParam("goodsStatus","sale");
		String keyword=getParam("keyword");
		String timeStart=getParam("timeStart");
		String timeEnd=getParam("timeEnd");
		//1.查询商品信息
		GoodsParamForManage param=new GoodsParamForManage(pageNo, keyword, categoryId,brandId,isSp, state, timeStart, timeEnd);
		param.setPageSize(pageSize);
		GoodsForManageVo page=goodsService.queryGoodsForManage(param);
		model.addAttribute("page", page);
		model.addAttribute("param", param);
		return "/center/groupGoods/goods_list";
	}
	/**
	 * 
	 *@Description  公共查询商品
	 *@param model
	 *@return 
	 *@author zcx
	 *@Time 2017年6月6日 上午9:49:47
	 */
	@RequestMapping("/queryGoodsList")
	public String queryGoodsList(Model model){
		Integer pageNo=getParam("pageNo", 1);
		String keyword=getParam("keyword");
		String chooseType = getParam("chooseType","one");
		GoodsParamForManage param=new GoodsParamForManage(pageNo, keyword, null, null, null, null, null, null);
		PageUtil<GoodsForManageInfo> page=goodsService.queryGoodsByParam(param);
		model.addAttribute("page", page);
		model.addAttribute("param", param);
		model.addAttribute("chooseType", chooseType);
		return "/center/public/goodsList";
	}
}
